package com.thomasmoerman.dci3.app

import org.hibernate._
import com.thomasmoerman.dci3.registry._
import com.thomasmoerman.dci3.domain._
import com.thomasmoerman.dci3.pbc._
import com.thomasmoerman.dci3.tmc._
import com.thomasmoerman.dci3.frw._

import org.springframework.context.annotation._

import scala.collection.JavaConversions._

object PayBillsContextTestApp extends Application {
  
  val registry = new AnnotationConfigApplicationContext(classOf[Registry])
  
  val dataAccessHelper = registry.getBean("dataAccessHelper").asInstanceOf[DataAccessHelper]
  
  val james = new Person("James", "Coplien")
  val trygve = new Person("Trygve", "Reenskaug")
  val rickard = new Person("Rickard", "Oberg")
  
  val account1 = new Account(james); james.account = account1
  val account2 = new Account(trygve); trygve.account = account2
  val account3 = new Account(rickard); rickard.account = account3
  
  val bill1 = new Bill(debtor = james, creditor = trygve, amount = 3, comment = "Sandwich")
  val bill2 = new Bill(debtor = james, creditor = trygve, amount = 50, comment = "Powerpoint slides")
  val bill3 = new Bill(debtor = james, creditor = rickard, amount = 100, comment = "Consultancy services")
  
  account1.increaseBalance(200)
  
  dataAccessHelper.execute { (session: Session) =>
    List(james, trygve, rickard, account1, account2, account3, bill1, bill2, bill3).foreach(session.persist)
  }
  
  val pbc = registry.getBean("payBillsContext").asInstanceOf[PayBillsContext]
  
  pbc.person = Some(james)
  
  pbc.execute
  
}